package com.dgshanger.sanhxiaofeisc.mvp.presenter;

import com.dgshanger.sanhxiaofeisc.entity.CheckPwdEntity;
import com.dgshanger.sanhxiaofeisc.entity.PayForDetailEntity;
import com.dgshanger.sanhxiaofeisc.entity.PayResultEntity;
import com.dgshanger.sanhxiaofeisc.mvp.contract.PayForDetailContract;
import com.dgshanger.sanhxiaofeisc.mvp.model.PayForDetailModel;
import com.dgshanger.sanhxiaofeisc.mvp.model.PayModelSettingModel;
import com.dgshanger.sanhxiaofeisc.mvp.model.VipCardModel;
import com.dgshanger.sanhxiaofeisc.ui.person.PayForDetailActivity;
import com.dgshanger.sanhxiaofeisc.ui.recharge.VipCardActivity;
import com.xx.baseuilibrary.mvp.BaseRxMvpPresenter;
import com.xx.baseutilslibrary.network.rx.RxHttpObserver;
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * PayForDetailPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/5/2 17:20.
 */

public class PayForDetailPresenter extends BaseRxMvpPresenter<PayForDetailContract.Model, PayForDetailContract.View> implements PayForDetailContract.Presenter {

    @NotNull
    @Override
    protected PayForDetailContract.Model createModel() {
        return new PayForDetailModel();
    }

    @Override
    public void loadDetail() {
        if (getView() == null) {
            return;
        }
        String msgId = getView().getMsgId();
        getModel().loadDetail(msgId, new RxHttpObserver<PayForDetailEntity>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable PayForDetailEntity entity) {
                if (getView() == null) {
                    return;
                }
                getView().setData(entity);
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() == null) {
                    getView().showToast(error);
                }
            }

            @Override
            public void onLoadingStart() {
                if (getView() == null) {
                    getView().showLoadingDialog();
                }
            }

            @Override
            public void onLoadingFinish() {
                if (getView() == null) {
                    getView().dismissLoadingDialog();
                }
            }
        });
    }

    @Override
    public void accept(String payType, String payPassword) {
        if (getView() == null) {
            return;
        }
        String msgId = getView().getMsgId();
        String extraId = getView().getExtraId();
        String type = getView().getType();
        if (PayForDetailEntity.PAY_FOR_PERSON.equals(type)) {
            //个人支付
            type = PayForDetailActivity.Companion.getTYPE_PERSON();
        } else if (PayForDetailEntity.PAY_FOR_BUSINESS.equals(type)) {
            //商家支付
            type = PayForDetailActivity.Companion.getTYPE_BUSINESS();
        }
        getModel().accept(extraId, type, payType, payPassword, new RxHttpObserver<PayResultEntity>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable PayResultEntity entity) {
                if (getView() == null || entity == null) {
                    return;
                }
                if (VipCardActivity.Companion.getPAY_ALIPAY().equals(payType)) {
                    //阿里支付
                    getView().createAliPay(entity.getAlipay_content());
                } else if (VipCardActivity.Companion.getPAY_WX().equals(payType)) {
                    //微信
                    String payReqString = new VipCardModel().getPayReqString(entity.getWx_content());
                    getView().createWeChatPay(payReqString);
                } else if (VipCardActivity.Companion.getPAY_WALLET().equals(payType)) {
                    //零钱
                    getView().showToast(msg);
                    getView().setAccpteUI();
                    getView().dismissPayDialog();
                }
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(error);
            }

            @Override
            public void onLoadingStart() {
                attachObserver(this);
                if (getView() == null) {
                    return;
                }
                getView().showLoadingDialog();
            }

            @Override
            public void onLoadingFinish() {
                if (getView() == null) {
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }

    @Override
    public void refuse() {
        if (getView() == null) {
            return;
        }
        String msgId = getView().getMsgId();
        String extraId = getView().getExtraId();
        getModel().refuse(msgId, new RxHttpObserver<Object>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable Object entity) {
                if (getView() == null) {
                    return;
                }
                getView().setRefuseUI();
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(error);
            }


            @Override
            public void onLoadingStart() {
                attachObserver(this);
                if (getView() == null) {
                    return;
                }
                getView().showLoadingDialog();
            }

            @Override
            public void onLoadingFinish() {
                if (getView() == null) {
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }

    @Override
    public void checkPayPassword() {
        new PayModelSettingModel().checkPwd(new XxBaseHttpObserver<CheckPwdEntity>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable CheckPwdEntity entity) {
                if (getView() == null) {
                    return;
                }
                //显示支付弹窗
                if ("0".equals(entity.getPwd_status())) {
                    //未设置密码
                    getView().showToast("请先到个人中心设置支付密码哦");
                } else {
                    getView().showPayDialog();
                }
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(error);
            }

            @Override
            public void onStart() {
                if (getView() == null) {
                    return;
                }
                getView().showLoadingDialog();
            }

            @Override
            public void onFinish() {
                if (getView() == null) {
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }
}
